package com.bocai.kelalacar.ui.car.set.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.CarDriverBean;
import com.bocai.kelalacar.bean.UploadImgBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.set.ada.CarManageAda;
import com.bocai.kelalacar.ui.car.set.contract.AddCarContract;
import com.bocai.kelalacar.ui.car.set.module.AddCarModule;
import com.bocai.kelalacar.ui.car.set.presenter.AddCarPresenter;
import com.bocai.kelalacar.util.SP;
import com.bocai.kelalacar.widget.DividerGridItemDecoration;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/22.
 */

public class CarManageActivity extends BaseActivity implements AddCarContract.view ,XRecyclerView.LoadingListener{
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.xrecycle)
    XRecyclerView xrecycle;
    CarManageAda ada;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @Inject
    AddCarPresenter presenter;
    @BindView(R.id.ll_back)
    LinearLayout llBack;


    @Override
    public int getLayoutId() {
        return R.layout.act_car_manage;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new AddCarModule(this)).inject(this);
    }

    @Override
    public void initView() {

        initToolbar("车辆管理");
        initBack(imgBack,llBack);
        llRight.setVisibility(View.VISIBLE);
        tvRight.setText("添加车辆");
        ada = new CarManageAda(this,presenter);
        xrecycle.setLayoutManager(new LinearLayoutManager(this));
        xrecycle.addItemDecoration(new DividerGridItemDecoration(this, R.color.colorWindow, 1));
        xrecycle.setAdapter(ada);

        xrecycle.setLoadingListener(this);
        xrecycle.setLoadingMoreEnabled(false);
    }

    @OnClick({R.id.ll_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_right:
                startActivity(AddCarActivity.class);
                break;
        }
    }

    @Override
    public void addCarSuccess() {

    }

    @Override
    public void uploadImgSuccess(UploadImgBean uploadImgBean) {

    }

    @Override
    public void optionalSeatsList(List<Integer> list) {

    }

    @Override
    public void getCarList(CarDriverBean bean) {
        xrecycle.refreshComplete();
        if (bean!=null&&bean.getContent()!=null){
        ada.clear();
        ada.addAll(bean.getContent().getCarList());
        }
    }

    @Override
    public void deleteCarSuccess(int index) {
        ada.getDataList().remove(index);
        ada.notifyDataSetChanged();
    }

    @Override
    public void modifyCarSuccess() {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }else if (msg.equals("网络异常")){
            xrecycle.refreshComplete();
            xrecycle.loadMoreComplete();
        }
    }

    @Override
    public void onRefresh() {
        presenter.getCarDriverList(SP.get(this,"id","").toString());
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        xrecycle.refresh();
    }
}
